package linuxbox.poll.servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.text.*;
import java.sql.*;

import linuxbox.bean.*;
import linuxbox.exception.*;
import linuxbox.util.*;
import linuxbox.BoxStatic;
import linuxbox.common.*;

import linuxbox.poll.logic.*;

public class BoxPollAction extends Action
{
	protected static String viewJSP = null;
	protected static String editJSP = null;
	protected static String voteJSP = null;
	protected static String concludeJSP = null;

  BoxPoll home = new BoxPoll();

  public void doAction(HttpSession session, BoxSession aBoxSession,
  HttpServletRequest req, HttpServletResponse resp) throws Exception
	{
    String str = (String) req.getParameter("VIEW_TYPE");
    if ( str != null && str.length() > 0 )
 		  session.setAttribute("poll.VIEW_TYPE", str);

    str = (String) req.getParameter("SORTBY");
    if ( str != null && str.length() > 0 )
 		  session.setAttribute("poll.SORTBY", str);

    str = (String) req.getParameter("ASC");
    if ( str != null && str.length() > 0 )
 		  session.setAttribute("poll.ASC", str);

    str = req.getParameter("SubmitCancel");
    if ( str != null )
    {
      sendFile(viewJSP,req,resp);
      return;
    }

    int curEntryID = 0;
    str = req.getParameter("ID");
    if ( str != null)
    {
      curEntryID = Integer.parseInt(str);
    }
    str = (String) req.getParameter("SubmitSave");
    if ( str != null )
    {
      saveToBean(curEntryID, req, aBoxSession);
      sendFile(viewJSP,req,resp);
      return;
    }
    str = req.getParameter("SubmitUpdate");
    if ( str != null )
    {
      saveToBean(curEntryID, req, aBoxSession);
      sendFile(viewJSP,req,resp);
      return;
    }
    str = req.getParameter("SubmitDelete");
    if ( str != null )
    {
      deleteBean(curEntryID);
      sendFile(viewJSP,req,resp);
      return;
    }
    str = req.getParameter("SubmitView");
    if ( str != null )
    {
      viewVotes(curEntryID, req, resp);
      return;
    }
    str = req.getParameter("SubmitVote");
    if ( str != null )
    {
      saveVotes(curEntryID, aBoxSession.getID(), req);
      viewVotes(curEntryID, req, resp);
      return;
    }
    str = req.getParameter("SubmitConclude");
    if ( str != null )
    {
      sendFile(concludeJSP,req,resp);
      return;
    }
    str = req.getParameter("SubmitEdit");
    if ( str != null )
    {
      // Now we'll go to the edit form
      if ( curEntryID == 0 )
      {
        setEntryDefaultAttributes(req);
      }
      else
      {
        setEntryAttributes(req, curEntryID);
      }
      sendFile(editJSP,req,resp);
      return;
    }
    else
      sendFile(viewJSP,req,resp);
  }

  protected boolean saveToBean(int id, HttpServletRequest req, BoxSession aBoxSession)
  {
    //TODO: Validate the data
    BoxPollEntry entry = null;
    if ( id == 0 )
      entry = home.add();
    else
      entry = home.get(new BaseIntKey(id));
    id = entry.getID();

    entry.setCreatedBy(aBoxSession.getID());
  //Form validate
    String[] choices = req.getParameterValues("CHOICES");
    if ( choices != null && choices.length < 2 )
    {
      //TODO: Log errors
      return false;
    }
    if (!home.updateChoices(id, choices))
      return false;

    String str = req.getParameter("SUBJECT");
    if ( str != null && str.length() > 0)
      entry.setSubject(str);
    else
      entry.setSubject("No Title");

    str = req.getParameter("DESCRIPTION");
    if ( str != null )
      entry.setDescription(str);

	  str = req.getParameter("MULTIPLE");
	  if ( str != null && str.length()>0 )
	  {
		  int i = Integer.parseInt(str);
		  entry.setMultiple(i>0);
	  }

	  str = req.getParameter("SHOWVOTER");
	  if ( str != null && str.length()>0 )
	  {
		  int i = Integer.parseInt(str);
		  entry.setShowVoter(i>0);
	  }

	  str = req.getParameter("SHOWRESULT");
	  if ( str != null && str.length()>0 )
	  {
		  int i = Integer.parseInt(str);
		  entry.setShowResult(i>0);
	  }

	  str = req.getParameter("SENDRESULT");
	  if ( str != null && str.length()>0 )
	  {
		  int i = Integer.parseInt(str);
		  entry.setSendResult(i);
	  }

	  str = req.getParameter("MANUALCONCLUDE");
	  if ( str != null && str.length()>0 )
	  {
		  int i = Integer.parseInt(str);
		  entry.setManualConclude(i>0);
    }
    if ( !entry.getManualConclude() )
    {
      Calendar tmpCal = Calendar.getInstance();
      int tmpYear = tmpCal.get(Calendar.YEAR);
      int tmpMonth = tmpCal.get(Calendar.MONTH);
      int tmpDay = tmpCal.get(Calendar.DAY_OF_MONTH);

      str = req.getParameter("END_YEAR");
      if ( str != null )
        tmpYear = Integer.parseInt(str);
      str = req.getParameter("END_MONTH");
      if ( str != null )
        tmpMonth = Integer.parseInt(str);
      str = req.getParameter("END_DAY");
      if ( str != null )
        tmpDay = Integer.parseInt(str);
      if ( tmpYear == -1 || tmpMonth == -1 || tmpDay == -1 )
        entry.setConcludeTime(new Timestamp(tmpCal.getTime().getTime()));
      else
      {
        tmpCal.set(tmpYear, tmpMonth, tmpDay, 0, 0, 0);
        entry.setConcludeTime(new Timestamp(tmpCal.getTime().getTime()));
      }
    }
    else
    {
      entry.setConcludeTime(null);
    }

    entry.setCreateTime(new Timestamp(new java.util.Date().getTime()));
    return home.update(entry);
  }

  protected void deleteBean(int id)
  {
    if ( id != 0 )
    {
      home.remove(new BaseIntKey(id));
    }
  }

  protected void setEntryAttributes(HttpServletRequest req, int ID)
  {
    BoxPollEntry entry = home.get(new BaseIntKey(ID));

    //Set the defalut attributes for a new entry
    req.setAttribute("ID", new Integer(entry.getID()).toString());
    req.setAttribute("SUBJECT", entry.getSubject());
    req.setAttribute("DESCRIPTION", entry.getDescription());

    req.setAttribute("MULTIPLE", entry.getMultiple()?"1":"0");
    req.setAttribute("SHOWVOTER", entry.getShowVoter()?"1":"0");
    req.setAttribute("SHOWRESULT", entry.getShowResult()?"1":"0");
    req.setAttribute("MANUALCONCLUDE", entry.getManualConclude()?"1":"0");
    req.setAttribute("SENDRESULT", new Integer(entry.getSendResult()).toString());

    Calendar tmp = Calendar.getInstance();
    if (entry.getConcludeTime() != null)
      tmp.setTime(entry.getConcludeTime());
    int tmpYear = tmp.get(Calendar.YEAR);
    int tmpMonth = tmp.get(Calendar.MONTH);
    int tmpDay = tmp.get(Calendar.DATE);
    req.setAttribute("END_YEAR", new Integer(tmpYear).toString());
		req.setAttribute("END_MONTH", new Integer(tmpMonth).toString());
		req.setAttribute("END_DAY", new Integer(tmpDay).toString());
  }

  protected void setEntryDefaultAttributes(HttpServletRequest req)
  {
    //Set the defalut attributes for a new entry
    req.setAttribute("ID", "0");
    req.setAttribute("SUBJECT", "");
    req.setAttribute("DESCRIPTION", "");
    req.setAttribute("MULTIPLE", "0");
    req.setAttribute("SHOWVOTER", "1");
    req.setAttribute("SHOWRESULT", "1");
    req.setAttribute("SENDRESULT", "0");
    req.setAttribute("MANUALCONCLUDE", "1");

    Calendar today = Calendar.getInstance();
    int tmpYear = today.get(Calendar.YEAR);
    int tmpMonth = today.get(Calendar.MONTH);
    int tmpDay = today.get(Calendar.DATE);
    req.setAttribute("END_YEAR", new Integer(tmpYear).toString());
		req.setAttribute("END_MONTH", new Integer(tmpMonth).toString());
		req.setAttribute("END_DAY", new Integer(tmpDay).toString());
  }

  public boolean saveVotes(int id, int userID, HttpServletRequest req)
  {
    String[] choices=req.getParameterValues("CHOICES");
    return home.updateVotes(userID,id, choices);
  }

  public void viewVotes(int id, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
  {
    if ( id == 0 )
      return;
    BoxPollEntry entry = home.get(new BaseIntKey(id));
    if ( entry == null )
    {
      //TODO: Log error
      return;
    }
    req.setAttribute("ID", new Integer(id).toString());
    req.setAttribute("SHOWVOTER", entry.getShowVoter()?"1":"0");
    req.setAttribute("SHOWRESULT", entry.getShowResult()?"1":"0");
    req.setAttribute("MULTIPLE", entry.getMultiple()?"1":"0");
    req.setAttribute("SUBJECT", entry.getSubject());
    req.setAttribute("DESCRIPTION", entry.getDescription());

    if ( entry.getClosed() )
    {
      req.setAttribute("VIEWONLY", "1");
      req.setAttribute("SHOWRESULT", "1");
    }
    else
      req.setAttribute("VIEWONLY", "0");
    sendFile(voteJSP,req,resp);
  }

	public void setViewJSP(String str)
	{
		viewJSP = str;
	}
	public void setEditJSP(String str)
	{
		editJSP = str;
	}
	public void setVoteJSP(String str)
	{
		voteJSP = str;
	}
	public void setConcludeJSP(String str)
	{
		concludeJSP = str;
	}
}
